package com.zzh.dp;

import java.util.Arrays;

public class No174_calculateMinimumHP {
    public static void main(String[] args) {
        No174Solution solution = new No174Solution();
        int i = solution.calculateMinimumHP(new int[][]{
                {-2, -3, 3},
                {-5, -10, 55},
                {10, 30, -5}
        });
        System.out.println(i);
    }
}

class No174Solution {
    public int calculateMinimumHP(int[][] dungeon) {
        int rowLen = dungeon.length;
        int colLen = dungeon[0].length;
        int[][] costs = new int[rowLen][colLen];

        for (int row = 0; row < rowLen; row++) {
            for (int col = 0; col < colLen; col++) {
                if (row==0&&col==0){
                    costs[row][col]=dungeon[row][col];
                }else if (row==0){
                    costs[row][col]=dungeon[row][col]+costs[row][col-1];
                }else if (col==0){
                    costs[row][col]=dungeon[row][col]+costs[row-1][col];
                }else {
//                    costs[row][col]=dungeon[row][col]+closeZero(costs[row-1][col],costs[row][col-1]);
//                    costs[row][col]=dungeon[row][col]+Math.max(costs[row-1][col],costs[row][col-1]);
                    costs[row][col]=closeZero(dungeon[row][col]+costs[row-1][col],dungeon[row][col]+costs[row][col-1]);
                }
            }
        }
        for (int i=0;i<rowLen;i++){
            System.out.println(Arrays.toString(costs[i]));
        }
        return costs[rowLen-1][colLen-1];
    }
    public int closeZero(int a,int b){
        return Math.abs(a)<Math.abs(b)?a:b;
    }
}
